package com.ml.carManager.utils;

import android.app.Activity;
import android.content.Context;

import androidx.annotation.NonNull;

import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.hjq.toast.ToastUtils;
import com.ml.carManager.ui.dialog.SimpleHintDialog;
import com.ml.carManager.ui.dialog.listener.OnEnterClickListener;

import java.util.List;

public class PermissionsUtils {

    /**
     * 请求权限
     */
    public static void requestPermission(Context context, String[] permissions, OnPermissionListener onPermissionListener) {
        if (permissions == null || permissions.length == 0) {
            return;
        }
        XXPermissions xxPermissions = XXPermissions.with(context);
        for (int i = 0; i < permissions.length; i++) {
            // 申请单个权限
            xxPermissions.permission(permissions[i]);
        }

        // 申请多个权限
        //        xxPermissions.permission(Permission.Group.ACCESS_COARSE_LOCATION);

        // 设置权限请求拦截器（局部设置）
        //xxPermissions.interceptor(new PermissionInterceptor())
        // 设置不触发错误检测机制（局部设置）
        //xxPermissions.unchecked()
        xxPermissions.request(new OnPermissionCallback() {

            @Override
            public void onGranted(@NonNull List<String> permissions, boolean allGranted) {
                if (!allGranted) {
//                            toast("获取部分权限成功，但部分权限未正常授予");
                    ToastUtils.show("权限获取失败，请检查");
                    if (onPermissionListener != null) {
                        onPermissionListener.onRequestFail();
                    }
                    return;
                }
                if (onPermissionListener != null) {
                    onPermissionListener.onRequestSuccess();
                }
//                        toast("获取录音和日历权限成功");
            }

            @Override
            public void onDenied(@NonNull List<String> permissions, boolean doNotAskAgain) {
                if (doNotAskAgain) {
//                            toast("被永久拒绝授权，请手动授予录音和日历权限");

                    SimpleHintDialog simpleHintDialog = new SimpleHintDialog(context);
                    simpleHintDialog.setTitleString("权限被拒绝");
                    simpleHintDialog.setContentString("需要您到设置页面手动设置权限，否则不能正常使用APP");
                    simpleHintDialog.setConfirmBtnText("跳转设置");
                    simpleHintDialog.setOnEnterClickListener(new OnEnterClickListener() {
                        @Override
                        public void onEnter() {
                            // 如果是被永久拒绝就跳转到应用权限系统设置页面
                            XXPermissions.startPermissionActivity(context, permissions);
                        }
                    });
                    simpleHintDialog.show();


                } else {
                    if (onPermissionListener != null) {
                        onPermissionListener.onRequestFail();
                    }
//                            toast("获取录音和日历权限失败");
                }
            }
        });

    }

    public interface OnPermissionListener {
        /**
         * 成功获取权限
         */
        void onRequestSuccess();

        void onRequestFail();
    }
}
